import { Controller } from "../../mvc/core/Controller";
import { WindowEvent } from "../../component/window/WindowEvent";
import { NpcEffectRule } from "../rules/NpcEffectRule";
import { NetDispatcher } from "../../mvc/core/NetDispatcher";
import { ServerCommand } from "../mvc/ServerCommand";
import { DataEvent } from "../events/DataEvent";
import { QuickBuyWindow } from "../view/common/QuickBuyWindow";
import { ECategory } from "../../../Message/Public/ECategory";
import { EProp } from "../../../Message/Public/EProp";
import { MsgManager } from "../manager/MsgManager";
import { Language } from "../../../extend/language/Language";
import { GameProxy } from "../mvc/GameProxy";
import { ETaskGroup } from "../../../Message/Game/ETaskGroup";
import { SectarianTaskInfo } from "../model/SectarianTaskInfo";
import { TaskInfo } from "../model/TaskInfo";
import { ETaskStatus } from "../../../Message/Game/ETaskStatus";
import { AIManager } from "../scene/ai/AIManager";
import { Alert } from "../../../com/mui/controls/Alert";
import { HTMLUtil } from "../../../com/gengine/utils/HTMLUtil";
import { ColorConfig } from "../resource/ColorConfig";
import { Dispatcher } from "../../mvc/core/Dispatcher";
import { EventName } from "../mvc/EventName";
import { GameManager } from "../manager/GameManager";
import { ModuleType } from "../view/common/ModuleType";
import { ISectarianTaskModule } from "../../../modules/interfaces/ISectarianTaskModule";
import { GameMapUtil } from "../scene/map/GameMapUtil";
import { Cache } from "../cache/Cache";
import { STask } from "../../../Message/Game/STask";
import { IView } from "../../mvc/interfaces/IView";
import { SectarianTaskModule } from "../../../modules/SectarianTaskModule";
type int = number;
//class SectarianTaskController
    
    export  class SectarianTaskController extends Controller
    {
       

        /* internal  */onWindowShowHandler(arg1: WindowEvent): void
        {
            NpcEffectRule.registDialogEffectWindow(view);
            NetDispatcher.addCmdListener(ServerCommand.TaskUpdate, this.onTaskUpdateHandler);
            this.onBackPackChange();
            return;
        }

        /* internal  */onWindowCloseHandler(arg1: WindowEvent): void
        {
            NetDispatcher.removeCmdListener(ServerCommand.TaskUpdate, this.onTaskUpdateHandler);
            return;
        }

        /* internal  */onFlushItemBuyReq(arg1: DataEvent): void
        {
            QuickBuyWindow.instance.updateWindow(ECategory._ECategoryProp, EProp._EPropSchoolToken, -1);
            QuickBuyWindow.instance.show();
            return;
        }

        /* internal  */onTaskWinOpenHandler(arg1: DataEvent): void
        {
            if (cache.role.entityInfo.level < cache.daily.schoolInfo.needLevel) 
            {
                MsgManager.showRollTipsMsg(Language.getStringByParam(20188, cache.daily.schoolInfo.needLevel));
                return;
            }
            if (view.isHide) 
            {
                GameProxy.sectarianTask.openSchoolTask(cache.dialog.npcInfo.tnpc.npcId);
            }
            else 
            {
                view.hide();
            }
            return;
        }

        /* internal  */onSectarianTaskGetReq(arg1: DataEvent): void
        {
            var loc1=cache.task.getTaskByGroup(ETaskGroup._ETaskGroupSchool).length;
            if (loc1 >= 3) 
            {
                MsgManager.showRollTipsMsg(Language.getString(20139));
                return;
            }
            if (cache.daily.schoolInfo.num >= cache.daily.schoolInfo.totalNum) 
            {
                MsgManager.showRollTipsMsg(Language.getStringByParam(20190, cache.daily.schoolInfo.totalNum));
                return;
            }
            var loc2=arg1.data as SectarianTaskInfo;
            if (loc2 != null) 
            {
                GameProxy.taskProxy.getTaskReq(cache.dialog.npcInfo.tnpc.npcId, loc2.task.code);
                GameProxy.sectarianTask.openSchoolTask(cache.dialog.npcInfo.tnpc.npcId);
            }
            return;
        }

        /* internal  */onSectarianTaskEndReq(arg1: DataEvent): void
        {
            var loc1=arg1.data as TaskInfo;
            if (loc1 != null) 
            {
                if (loc1.status != ETaskStatus._ETaskStatusNotCompleted) 
                {
                    GameProxy.taskProxy.endTaskReq(cache.dialog.npcInfo.tnpc.npcId, loc1.sTask.code);
                    GameProxy.sectarianTask.openSchoolTask(cache.dialog.npcInfo.tnpc.npcId);
                }
                else 
                {
                    AIManager.onAutoPathAIControl(loc1.getTargetByIndex(0));
                }
            }
            return;
        }

        /* internal  */onSectarianTaskFlushReq(arg1: DataEvent): void
        {
            this._flushItemCode = 0;
            this._autoBuy = false;
            var loc1=cache.sectarian.hasMaxColorCanGet();
            if (loc1) 
            {
                Alert.buttonWidth = 60;
                Alert.show(Language.getStringByParam(20191, HTMLUtil.addColor(Language.getString(20192), ColorConfig.instance.getItemColor(5).color)), Language.getString(20055), Alert.YES | Alert.NO, null, this.onFlushTaskhasMaxColor);
            }
            else 
            {
                this.onFlushTaskhasMaxColor(Alert.YES);
            }
            return;
        }

        /* internal  */onSectarianTaskFlushByItemReq(arg1: DataEvent): void
        {
            var loc1=false;
            var loc3=null;
            this._flushItemCode = 0;
            this._autoBuy = Boolean(arg1.data);
            var loc2=cache.pack.backPackCache.getTypeItems(ECategory._ECategoryProp, EProp._EPropSchoolToken);
            if (loc2 && loc2.length > 0) 
            {
                loc3 = loc2[0];
                this._flushItemCode = loc3.itemInfo.item.code;
                loc1 = cache.sectarian.hasMaxColorCanGet();
                if (loc1) 
                {
                    Alert.buttonWidth = 60;
                    Alert.show(Language.getStringByParam(20191, HTMLUtil.addColor(Language.getString(20192), ColorConfig.instance.getItemColor(5).color)), Language.getString(20055), Alert.YES | Alert.NO, null, this.onFlushTaskhasMaxColor);
                }
                else 
                {
                    this.onFlushTaskhasMaxColor(Alert.YES);
                }
            }
            else if (this._autoBuy) 
            {
                loc1 = cache.sectarian.hasMaxColorCanGet();
                if (loc1) 
                {
                    Alert.buttonWidth = 60;
                    Alert.show(Language.getStringByParam(20191, HTMLUtil.addColor(Language.getString(20192), ColorConfig.instance.getItemColor(5).color)), Language.getString(20055), Alert.YES | Alert.NO, null, this.onFlushTaskhasMaxColor);
                }
                else 
                {
                    this.onFlushTaskhasMaxColor(Alert.YES);
                }
            }
            else 
            {
                MsgManager.showRollTipsMsg(Language.getStringByParam(20193, HTMLUtil.addColor(Language.getString(20184), "#00ff00")));
            }
            return;
        }

        /* internal  */onTaskQuickEndHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as TaskInfo;
            if (loc1) 
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.TaskQuickComAndEndReq, loc1));
            }
            return;
        }

        /* internal  */onTaskQuickEndResHandler(arg1: DataEvent): void
        {
            GameProxy.sectarianTask.openSchoolTask(cache.dialog.npcInfo.tnpc.npcId);
            return;
        }

        /* internal  */onItemBuySuccess(): void
        {
            flash.utils.setTimeout(this.onItemBuySuccessTimeOut, 300);
            return;
        }

        /* internal  */onItemBuySuccessTimeOut(): void
        {
            var loc3=null;
            var loc1=cache.pack.backPackCache.getTypeItems(ECategory._ECategoryProp, EProp._EPropSchoolToken);
            if (loc1 && loc1.length > 0) 
            {
                loc3 = loc1[0];
                this._flushItemCode = loc3.itemInfo.item.code;
            }
            var loc2=cache.sectarian.hasMaxColorCanGet();
            if (loc2) 
            {
                Alert.buttonWidth = 60;
                Alert.show(Language.getStringByParam(20191, HTMLUtil.addColor(Language.getString(20192), ColorConfig.instance.getItemColor(5).color)), Language.getString(20055), Alert.YES | Alert.NO, null, this.onFlushTaskhasMaxColor);
            }
            else 
            {
                this.onFlushTaskhasMaxColor(Alert.YES);
            }
            return;
        }

        /* internal  */onBuyFlushItemAlert(arg1: int): void
        {
            if (arg1 == Alert.OK) 
            {
                if (cache.guild.myGuildInfo) 
                {
                    Dispatcher.dispatchEvent(new DataEvent(EventName.GuildOpenShop));
                }
                else 
                {
                    Alert.show(Language.getStringByParam(20194, HTMLUtil.addColor(Language.getString(20195), "#ffff00")), Language.getString(20055), Alert.OK | Alert.CANCEL, null, this.onApplyGuildAlert);
                }
            }
            return;
        }

        /* internal  */onApplyGuildAlert(arg1: int): void
        {
            if (arg1 == Alert.OK) 
            {
                GameManager.instance.popupWindow(ModuleType.Guild);
            }
            return;
        }

        /* internal  */onFlushTaskhasMaxColor(arg1: int): void
        {
            if (arg1 != Alert.YES) 
            {
                (view as ISectarianTaskModule).updateFlushBtnEnabled(true);
            }
            else 
            {
                (view as ISectarianTaskModule).updateFlushBtnEnabled(false);
                GameProxy.sectarianTask.flushSchoolTask(cache.dialog.npcInfo.tnpc.npcId, this._flushItemCode, this._autoBuy);
            }
            return;
        }

        /* internal  */onTaskOpenRes(arg1: Object): void
        {
            if (GameMapUtil.curMapState.isCrossCountry) 
            {
                if (Cache.instance.dialog.getNPCTalk(cache.dialog.npcInfo) == "" || Cache.instance.dialog.getNPCTalk(cache.dialog.npcInfo) == null) 
                {
                    if (!(_view == null) && !_view.isHide) 
                    {
                        _view.hide();
                    }
                    return;
                }
            }
            if (view.isHide) 
            {
                view.show();
            }
            (view as ISectarianTaskModule).updateData(cache.sectarian.canGetList, cache.sectarian.currentList, cache.dialog.npcInfo);
            return;
        }

        /* internal  */onTaskFlushRes(arg1: Object): void
        {
            if (_view && !_view.isHide) 
            {
                (view as ISectarianTaskModule).flushTask(cache.sectarian.canGetList);
            }
            return;
        }

        /* internal  */onTaskCancelRes(arg1: STask): void
        {
            if (_view && !_view.isHide) 
            {
                if (arg1 && arg1.group == ETaskGroup._ETaskGroupSchool) 
                {
                    GameProxy.sectarianTask.openSchoolTask(cache.dialog.npcInfo.tnpc.npcId);
                }
            }
            return;
        }

        /* internal  */onTaskUpdateHandler(arg1: Array<any>): void
        {
            var loc1=false;
            var loc2=null;
            if (_view && !_view.isHide) 
            {
                var loc3=0;
                var loc4=arg1;
                for(loc2 of loc4) 
                {
                    if (loc2.sTask.group != ETaskGroup._ETaskGroupSchool) 
                    {
                        continue;
                    }
                    loc1 = true;
                }
                if (loc1) 
                {
                    cache.sectarian.updateCurTask();
                    (view as ISectarianTaskModule).updateData(cache.sectarian.canGetList, cache.sectarian.currentList, cache.dialog.npcInfo);
                }
            }
            return;
        }

        /* internal  */onBackPackChange(arg1: Object=null): void
        {
            if (_view && !_view.isHide) 
            {
                (view as ISectarianTaskModule).updateFlushItem(cache.pack.backPackCache.getItemCountByCategoryAndType(ECategory._ECategoryProp, EProp._EPropSchoolToken));
            }
            return;
        }

        protected /* override */ initServer(): void
        {
            super.initServer();
            Dispatcher.addEventListener(EventName.SectarianTaskWinOpen, this.onTaskWinOpenHandler);
            Dispatcher.addEventListener(EventName.SectarianTaskGetReq, this.onSectarianTaskGetReq);
            Dispatcher.addEventListener(EventName.SectarianTaskEndReq, this.onSectarianTaskEndReq);
            Dispatcher.addEventListener(EventName.SectarianTaskFlushReq, this.onSectarianTaskFlushReq);
            Dispatcher.addEventListener(EventName.SectarianTaskFlushByItemReq, this.onSectarianTaskFlushByItemReq);
            NetDispatcher.addCmdListener(ServerCommand.SectarianTaskOpenRes, this.onTaskOpenRes);
            NetDispatcher.addCmdListener(ServerCommand.SectarianTaskFlushRes, this.onTaskFlushRes);
            NetDispatcher.addCmdListener(ServerCommand.TaskCancelRes, this.onTaskCancelRes);
            Dispatcher.addEventListener(EventName.SectarianTaskQuickEndReq, this.onTaskQuickEndHandler);
            Dispatcher.addEventListener(EventName.SectarianTaskQuickEndRes, this.onTaskQuickEndResHandler);
            return;
        }

        protected /* override */ initView(): IView
        {
            var loc1=new SectarianTaskModule();
            loc1.addEventListener(WindowEvent.SHOW, this.onWindowShowHandler);
            loc1.addEventListener(WindowEvent.CLOSE, this.onWindowCloseHandler);
            loc1.addEventListener(EventName.SectarianTaskFlushItemBuy, this.onFlushItemBuyReq);
            return loc1;
        }

        private /* var */_flushItemCode: int;

        private /* var */_autoBuy: Boolean;
    }
